home *** CD-ROM | disk | FTP | other *** search
/ Maclife 35 / MACLIFE35.ISO.7z / MACLIFE35.ISO / 各社提供ソフト / Netscape Communicator PPC.sit / Netscape Communicator Folder / NetHelp / CntTool.js < prev    next >
Text File  |  1997-06-24  |  12KB  |  741 lines

  1. /* ==================================================================
  2.  
  3. FILE:   CntTool.js
  4.  
  5. DESCR:  Contents control library file for Netscape Help implementation.
  6.  
  7. NOTES:  Requires Utility.js
  8.  
  9. ================================================================== */
  10.  
  11. var ASSERT = true
  12.  
  13.  
  14.  
  15. var contentsObj
  16.  
  17.  
  18.  
  19. var aDatasets      = new Array()
  20.  
  21. var aTmpContainers = new Array()
  22.  
  23. var aTmpItems      = new Array()
  24.  
  25.  
  26.  
  27. /*
  28.  
  29. DESCR:   Contents class.
  30.  
  31. PARAMS:  bgcolor  Background color for the Contents tool.
  32.  
  33. RETURNS: 
  34.  
  35. NOTES:   
  36.  
  37. */
  38.  
  39. function contents( bgcolor )
  40.  
  41. {
  42.  
  43.    //top.SystemFrame.trace( "contents construct" )
  44.  
  45.    //alert( "contents construct" )
  46.  
  47.  
  48.  
  49.    this.bgcolor = bgcolor
  50.  
  51.  
  52.  
  53.    this.lastToggledContainerIndex
  54.  
  55.  
  56.  
  57.    this.bContainerToggle = false
  58.  
  59.    this.bItemSelected    = false
  60.  
  61.    this.currentDataset   = ""
  62.  
  63.  
  64.  
  65.    this.setDataset     = setDataset
  66.  
  67.    this.updateTree     = updateTree
  68.  
  69.    this.updateEntries  = updateEntries
  70.  
  71.    this.writeDocument  = writeDocument
  72.  
  73.    this.scrollDocument = scrollDocument
  74.  
  75.    this.containerClick = containerClick
  76.  
  77.    this.itemClick      = itemClick
  78.  
  79.  
  80.  
  81.    // Set global reference to this component.
  82.  
  83.    contentsObj = this
  84.  
  85.  
  86.  
  87.    // Load data.
  88.  
  89.    loadData()
  90.  
  91. }
  92.  
  93.  
  94.  
  95.    /*
  96.  
  97.    DESCR:   Selects a dataset for the tool.
  98.  
  99.    PARAMS:  indexName  The name that indexes the dataset in the
  100.  
  101.                        datasets array.
  102.  
  103.    RETURNS: 
  104.  
  105.    NOTES:   
  106.  
  107.    */
  108.  
  109.    function setDataset( indexName )
  110.  
  111.    {
  112.  
  113.       //alert( "setDataset, " + indexName )
  114.  
  115.       //top.SystemFrame.trace( "contents setDataset()" )
  116.  
  117.  
  118.  
  119.       this.currentDataset = indexName
  120.  
  121.    }
  122.  
  123.  
  124.  
  125.    /*
  126.  
  127.    DESCR:   Updates the Contents tree.
  128.  
  129.    PARAMS:  newURL  The current topic. Passing a URL expands its container
  130.  
  131.             as needed and selects the item representing the URL. If updating
  132.  
  133.             to just to toggle a container, pass null.
  134.  
  135.    RETURNS: 
  136.  
  137.    NOTES:   
  138.  
  139.    */
  140.  
  141.    function updateTree( newURL )
  142.  
  143.    {
  144.  
  145.       //top.SystemFrame.trace( "contents updateTree()" )
  146.  
  147.       //alert( "updateTree" )
  148.  
  149.  
  150.  
  151.       assert( ( this.currentDataset != "" ), DATASET )
  152.  
  153.  
  154.  
  155.       // Write a background color to the hidden frame, and bind its events to
  156.  
  157.       // the global handlers.
  158.  
  159.       frames[ 0 ].document.open()
  160.  
  161.       var html = "<BODY BGCOLOR = " + this.bgcolor + ">"
  162.  
  163.       html += "<SCRIPT LANGUAGE = 'JavaScript1.2'>"
  164.  
  165.       html += "top.bindDocEvts( document )"
  166.  
  167.       html += "</SCRIPT></BODY>"
  168.  
  169.       frames[ 0 ].document.write( html )
  170.  
  171.       frames[ 0 ].document.close()
  172.  
  173.  
  174.  
  175.       // Bind hidden frame events to global event handlers.
  176.  
  177.       top.bindDocEvts( frames[ 0 ].document )
  178.  
  179.  
  180.  
  181.       // Determine if this is an update just to toggle a container.
  182.  
  183.       this.bContainerToggle = ( ( typeof( newURL ) == "undefined" ) ? true : false )
  184.  
  185.  
  186.  
  187.       // Update the entry objects.
  188.  
  189.       if ( !this.bContainerToggle ) this.updateEntries( newURL )
  190.  
  191.  
  192.  
  193.       // Write the tree.
  194.  
  195.       this.writeDocument()
  196.  
  197.  
  198.  
  199.       // Bind event handlers.
  200.  
  201.       top.bindDocEvts( ContentsFrame.document )
  202.  
  203.  
  204.  
  205.       // Scroll the tree.
  206.  
  207.       if ( this.bItemSelected ) this.scrollDocument()
  208.  
  209.    }
  210.  
  211.  
  212.  
  213.    /*
  214.  
  215.    DESCR:   Updates the tree entry objects.
  216.  
  217.    PARAMS:  newURL  The URL from updateTree().
  218.  
  219.    RETURNS: 
  220.  
  221.    NOTES:  
  222.  
  223.    */
  224.  
  225.    function updateEntries( newURL )
  226.  
  227.    {
  228.  
  229.       //top.SystemFrame.trace( "contents updateEntries()" )
  230.  
  231.       //alert("updateEntries()")
  232.  
  233.  
  234.  
  235.       var aTmpContainers = aDatasets[ this.currentDataset ]
  236.  
  237.  
  238.  
  239.       // Check each item...
  240.  
  241.       var itemsSelected = 0  // Assert only.
  242.  
  243.       this.bItemSelected = false
  244.  
  245.       for ( var i = 0; i < aTmpContainers.length; i++ ) {
  246.  
  247.          for ( var j = 0; j < aTmpContainers[ i ].aItems.length; j++ ) {
  248.  
  249.             with ( aTmpContainers[ i ].aItems[ j ] ) {
  250.  
  251.                
  252.  
  253.                // ...and turn off any that are selected...
  254.  
  255.                if ( bSelected ) bSelected = false
  256.  
  257.  
  258.  
  259.                // ...and select the specified item...
  260.  
  261.                if ( URLsSansPathsAreSame( URL, newURL ) ) {
  262.  
  263.  
  264.  
  265.                   itemsSelected++
  266.  
  267.                   assert( ( itemsSelected < 2 ), ITEMS_SELECTED )
  268.  
  269.  
  270.  
  271.                   bSelected = true
  272.  
  273.                   this.bItemSelected = true
  274.  
  275.  
  276.  
  277.                   // ...making sure the selected item's container is expanded.
  278.  
  279.                   aTmpContainers[ i ].bExpanded = true
  280.  
  281.                }
  282.  
  283.             }
  284.  
  285.          }
  286.  
  287.       }
  288.  
  289.    }
  290.  
  291.  
  292.  
  293.    /*
  294.  
  295.    DESCR:   Scrolls the tree.
  296.  
  297.    PARAMS:  
  298.  
  299.    RETURNS: 
  300.  
  301.    NOTES:   If update is triggered outside of tool, we want to scroll as
  302.  
  303.             needed to the selected item to show where user is in the tree.
  304.  
  305.             Same for an item click, so the item is visible. For a container
  306.  
  307.             click, we only want to scroll to the clicked container, since
  308.  
  309.             the user is just looking for, or covering up, items.
  310.  
  311.    */
  312.  
  313.    function scrollDocument()
  314.  
  315.    {
  316.  
  317.       //top.SystemFrame.trace( "contents scrollDocument()" )
  318.  
  319.       //alert("scrollDocument")
  320.  
  321.  
  322.  
  323.       // Ignore scrolling if content does not exceed window.
  324.  
  325.       if ( frames[ 1 ].document.height < frames[ 1 ].innerHeight ) return
  326.  
  327.  
  328.  
  329.       // Calculate our best guess of pixel magnitude along the y axis of tree
  330.  
  331.       // window for the item we want to make visible.
  332.  
  333.       var aTmpContainers  = aDatasets[ this.currentDataset ]
  334.  
  335.       var y               = 0
  336.  
  337.       var containerHeight = 11
  338.  
  339.       var itemHeight      = 11
  340.  
  341.       var topMargin       = 4
  342.  
  343.       var bottomMargin    = 4
  344.  
  345.       var wrapLeading     = 4
  346.  
  347.       var charPerLine     = 14
  348.  
  349.             
  350.  
  351.       // Tally pixels for entries.
  352.  
  353.       for ( var i = 0; i < aTmpContainers.length; i++ ) {
  354.  
  355.  
  356.  
  357.          // Tally for the container. Tally must account for how
  358.  
  359.          // many lines the entry takes up, including the spacing between
  360.  
  361.          // lines that wrap.
  362.  
  363.          var lines = Math.ceil( aTmpContainers[ i ].text.length / charPerLine )
  364.  
  365.          y += topMargin +
  366.  
  367.               ( containerHeight * lines ) +
  368.  
  369.               ( wrapLeading * ( lines - 1 ) ) +
  370.  
  371.               bottomMargin
  372.  
  373.  
  374.  
  375.          // If just toggling a container, tally only to the newly
  376.  
  377.          // toggled container.
  378.  
  379.          if ( this.bContainerToggle && this.lastToggledContainerIndex == i ) {
  380.  
  381.             break
  382.  
  383.          }
  384.  
  385.  
  386.  
  387.          // Tally items in opened containers.
  388.  
  389.          if ( aTmpContainers[ i ].bExpanded ) {
  390.  
  391.             for ( var j = 0; j < aTmpContainers[ i ].aItems.length; j++ ) {
  392.  
  393.                
  394.  
  395.                // Tally for the item.
  396.  
  397.                var tmpItemLength = aTmpContainers[ i ].aItems[ j ].text.length
  398.  
  399.                lines = Math.ceil( tmpItemLength / charPerLine )
  400.  
  401.                y += topMargin +
  402.  
  403.                     ( itemHeight * lines ) +
  404.  
  405.                     ( wrapLeading * ( lines - 1 ) ) +
  406.  
  407.                     bottomMargin
  408.  
  409.  
  410.  
  411.                // If updating for a newly selected item, stop all tallying
  412.  
  413.                // when we get to the selected item.
  414.  
  415.                if ( !this.bContainerToggle &&
  416.  
  417.                     aTmpContainers[ i ].aItems[ j ].bSelected ) {
  418.  
  419.                   i = ( aTmpContainers.length )
  420.  
  421.                   break
  422.  
  423.                }
  424.  
  425.             }
  426.  
  427.          }
  428.  
  429.       }
  430.  
  431.  
  432.  
  433.       // Scroll to bring the entry to the middle of the window.
  434.  
  435.       var frameHeight = frames[ 1 ].innerHeight
  436.  
  437.       var center = ( frameHeight / 2 )
  438.  
  439.       if ( y > center ) frames[ 1 ].scrollTo( 0, y - center )
  440.  
  441.    }
  442.  
  443.  
  444.  
  445.    /*
  446.  
  447.    DESCR:   Writes the tree document.
  448.  
  449.    PARAMS:  
  450.  
  451.    RETURNS: 
  452.  
  453.    NOTES:   
  454.  
  455.    */
  456.  
  457.    function writeDocument()
  458.  
  459.    {
  460.  
  461.       //top.SystemFrame.trace( "contents writeDocument()" )
  462.  
  463.       //alert("writeDocument")
  464.  
  465.  
  466.  
  467.       var aTmpContainers = aDatasets[ this.currentDataset ]
  468.  
  469.  
  470.  
  471.       var link
  472.  
  473.       var html = "<HTML><HEAD>"
  474.  
  475.  
  476.  
  477.       html += "<STYLE TYPE = 'text/javascript'>"
  478.  
  479.  
  480.  
  481.       html += "classes.container.a.fontFamily = 'arial';"
  482.  
  483.       html += "classes.container.a.fontSize = '12px';"
  484.  
  485.       html += "classes.container.a.marginTop = 4;"
  486.  
  487.       html += "classes.container.a.marginBottom = 4;"
  488.  
  489.       //html += "classes.container.a.textDecoration = 'none';"
  490.  
  491.  
  492.  
  493.       html += "classes.unselectedItem.a.fontFamily = 'arial';"
  494.  
  495.       html += "classes.unselectedItem.a.fontSize = '12px';"
  496.  
  497.       html += "classes.unselectedItem.a.marginLeft = 4;"
  498.  
  499.       html += "classes.unselectedItem.a.marginTop = 4;"
  500.  
  501.       html += "classes.unselectedItem.a.marginBottom = 4;"
  502.  
  503.       html += "classes.unselectedItem.a.color = '#6600ff';"
  504.  
  505.  
  506.  
  507.       html += "classes.selectedItem.a.fontFamily = 'arial';"
  508.  
  509.       html += "classes.selectedItem.a.fontSize = '12px';"
  510.  
  511.       html += "classes.selectedItem.a.marginLeft = 4;"
  512.  
  513.       html += "classes.selectedItem.a.marginTop = 4;"
  514.  
  515.       html += "classes.selectedItem.a.marginBottom = 4;"
  516.  
  517.       html += "classes.selectedItem.a.color = '#6600ff';"
  518.  
  519.       html += "classes.selectedItem.a.fontWeight = 'bold';"
  520.  
  521.       //html += "classes.selectedItem.a.backgroundColor = 'white';"
  522.  
  523.  
  524.  
  525.       // Remove href underlining.
  526.  
  527.       html += "tags.a.textDecoration = 'none';"
  528.  
  529.  
  530.  
  531.       html += "</STYLE>"
  532.  
  533.  
  534.  
  535.       html += "</HEAD><BODY BGCOLOR = " + this.bgcolor +
  536.  
  537.               " LINK = '#000066' ALINK = '#000066' VLINK = '#000066'>"
  538.  
  539.  
  540.  
  541.       for ( var i = 0; i < aTmpContainers.length; i++ ) {
  542.  
  543.  
  544.  
  545.          link = "¥"javascript:parent.contentsObj.containerClick('" + this.currentDataset + "', " + i + ")¥""
  546.  
  547.          html += "<A CLASS = 'container' HREF = " + link + ">" + aTmpContainers[ i ].text + "</A>"
  548.  
  549.  
  550.  
  551.          if ( aTmpContainers[ i ].bExpanded ) {
  552.  
  553.             for ( var j = 0; j < aTmpContainers[ i ].aItems.length; j++ ) {
  554.  
  555.                with ( aTmpContainers[ i ].aItems[ j ] ) {
  556.  
  557.  
  558.  
  559.                   var ssClass = ( bSelected ? "selectedItem" : "unselectedItem" )
  560.  
  561.                   link = "¥"javascript:parent.contentsObj.itemClick('" + this.currentDataset + "', " + i + ", " + j + ")¥""
  562.  
  563.                   html += "<A CLASS = '" + ssClass + "' HREF = " + link + ">" + text + "</A>"
  564.  
  565.                }
  566.  
  567.             }
  568.  
  569.          }
  570.  
  571.       }
  572.  
  573.  
  574.  
  575.       html += "</BODY></HTML>"
  576.  
  577.  
  578.  
  579.       with ( frames[ 1 ].document ) {
  580.  
  581.          open()
  582.  
  583.          write( html )
  584.  
  585.          close()
  586.  
  587.       }
  588.  
  589.    }
  590.  
  591.  
  592.  
  593.    /*
  594.  
  595.    DESCR:   Container object click "event."
  596.  
  597.    PARAMS:  datasetIndex    The dataset index.
  598.  
  599.             containerIndex  The container index.
  600.  
  601.    RETURNS: 
  602.  
  603.    NOTES:   
  604.  
  605.    */
  606.  
  607.    function containerClick( datasetIndex, containerIndex )
  608.  
  609.    {
  610.  
  611.       // Toggle the container's state.
  612.  
  613.       var obj = aDatasets[ datasetIndex ][ containerIndex ]
  614.  
  615.       obj.bExpanded = ( obj.bExpanded ? false : true )
  616.  
  617.       this.lastToggledContainerIndex = containerIndex
  618.  
  619.             
  620.  
  621.       // Update the tree.
  622.  
  623.       this.updateTree()
  624.  
  625.    }
  626.  
  627.  
  628.  
  629.    
  630.  
  631.    /*
  632.  
  633.    DESCR:   Item object click "event."
  634.  
  635.    PARAMS:  datasetIndex    The dataset index.
  636.  
  637.             containerIndex  The container index.
  638.  
  639.             itemIndex       The item index.
  640.  
  641.    RETURNS: 
  642.  
  643.    NOTES:   
  644.  
  645.    */
  646.  
  647.    function itemClick( datasetIndex, containerIndex, itemIndex )
  648.  
  649.    {
  650.  
  651.       // Notify component owner of selection, and update tree on success.
  652.  
  653.       var obj = aDatasets[ datasetIndex ][ containerIndex ].aItems[ itemIndex ]
  654.  
  655.       
  656.  
  657.       // Load the topic (location doesn't matter since it's a nethelp URL.
  658.  
  659.       location = obj.netHelpURL
  660.  
  661.    }
  662.  
  663.  
  664.  
  665.  
  666.  
  667. // End class definition: contents.
  668.  
  669.  
  670.  
  671. /*
  672.  
  673. DESCR:   Container class.
  674.  
  675. PARAMS:  text    The container's text.
  676.  
  677.          aItems  Array of item objects.
  678.  
  679. RETURNS: 
  680.  
  681. NOTES:   
  682.  
  683. */
  684.  
  685. function container( text, aItems )
  686.  
  687. {
  688.  
  689.    this.text     = text
  690.  
  691.    this.aItems   = aItems
  692.  
  693.  
  694.  
  695.    this.bExpanded = false
  696.  
  697. }
  698.  
  699.  
  700.  
  701. // End class definition: container.
  702.  
  703.  
  704.  
  705. /*
  706.  
  707. DESCR:   Item class.
  708.  
  709. PARAMS:  text        The item's text.
  710.  
  711.          URL         The URL in non-NetHelp form.
  712.  
  713.          netHelpURL  The URL in NetHelp form.
  714.  
  715. RETURNS: 
  716.  
  717. NOTES:   
  718.  
  719. */
  720.  
  721. function item( text, URL, netHelpURL )
  722.  
  723. {
  724.  
  725.    this.text       = text
  726.  
  727.    this.URL        = URL
  728.  
  729.    this.netHelpURL = netHelpURL
  730.  
  731.  
  732.  
  733.    this.bSelected = false
  734.  
  735. }
  736.  
  737.  
  738.  
  739. // End class definition: item.
  740.  
  741.